<!--
 * @Author: 偻儸小卒[EdisonLiu_]
 * @Date: 1970-01-01 08:00:00
 * @LastEditors: 偻儸小卒[EdisonLiu_]
 * @LastEditTime: 2020-03-23 14:30:03
 * @Description: 
 -->
<template>
  <div >
        <main>
         <div>
              <ul>
                <li v-for="note in note_texts" :key="note._id" >{{ note }}</li>
              </ul>
            </div>
            <input name='note_title' v-model="note_title" />


<el-row>
          <el-col :span="12" >
              <el-input type="textarea" v-model="note_text" :autosize="{ minRows: 20, maxRows: 80}" placeholder="请输入内容" >
              </el-input>
          </el-col>
          <el-col :span="12" >
              <div v-html="preview_note" class="text-left">

              </div>
          </el-col>
      </el-row>

            <button @click="add_note">添加</button>
        </main>
      </div>
    
</template>

<script>
import common from './common'

import noteDb from "../../datastore/note";
import showdown from 'showdown'
let converter = new showdown.Converter();
export default {
  name: "index",
   components: {
  },
  data() {
    return {
      note_title:"",
      note_text: "",
      note_texts: [],
      preview_note:'',
    };
  },
  created: function() {
    this.getnotes();
    
  },
   watch:{
        'note_text':'contentChanged'
    },  
  methods: {
        contentChanged(){ 
           this.preview_note = converter.makeHtml(this.note_text);
           console.log( this.preview_note)
        },
    //添加便签
    add_note() {
      let that = this;
      let add_time = parseInt(new Date().getTime() / 1000);
      noteDb.insert(
        {title:that.note_title, text: that.note_text, is_show: 1, add_time: add_time },
        function(err, newDoc) {
          // Callback is optional
          // newDoc is the newly inserted document, including its _id
          // newDoc has no key called notToBeSaved since its value was undefined
          that.getnotes();
        }
      );
    },
    async getnotes() {
      let that = this;
      noteDb
        .find({})
        .sort({ add_time: -1 })
        .exec(function(err, docs) {
          if (docs.length > 0) {
            that.note_texts = docs;
            //发送消息给主进程进行窗体生成
            that.$electron.ipcRenderer.send("new_notewindow");
          }
        });
    }
  }
};
</script>
